/*
 * Copyright (c) 2012 RTCLab
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 *    of conditions and the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 */

package com.rtckit {
	
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.utils.Timer;
import flash.events.TimerEvent;
	
public class Media {
	protected var serverStream:NetStream = null;
	protected var peerStream:NetStream = null;
	
	protected var nc:NetConnection;
	protected var config:SoftphoneConfig;

	private var peerID:String = null;
	private var peerConnectionTimer:Timer;

	function Media(nc:NetConnection, config:SoftphoneConfig) {
		this.nc = nc;
		this.config = config;
		
		peerConnectionTimer = new Timer(3000, 1);
		peerConnectionTimer.addEventListener(flash.events.TimerEvent.TIMER, checkPeerConnection);
	}
	
	protected function waitPeerConnection(peerID:String):void {
		this.peerID = peerID;
		if (peerID) {
			peerConnectionTimer.reset();
			peerConnectionTimer.start();
		}
	}
	
	public function peerConnected(peerID:String):void {
		if (this.peerID == peerID) {
			this.peerID = null;
			Logger.debug('Peer connected');
		} else {
			Logger.debug('Wrong peer');
		}
	}
	
	private function checkPeerConnection(event:TimerEvent):void {
		if (!peerID)
			return;
		peerID = null;
		Logger.debug('Peer not connected: switching to server connection')
		onPeerConnectionFailed();
	}
	
	public function onPeerConnectionFailed():void {}
	
	public function currentStream():NetStream {
		return null;
	}
	
	public function getAudioLossRate():Number {
		var stream:NetStream = currentStream();
		if (!stream)
			return -1;
		return stream.info.audioLossRate;
	}

}
	
	
}